#include <iostream>

//类模板:模板化的是成员变量
template<typename T>
class DynamicArray 
{
public:
    DynamicArray();
    ~DynamicArray();
    void InsertTail(const T& element);
    
    DynamicArray& operator=(const DynamicArray& a);
    //友元模板的声明不能和类模板重名
    template<typename U>
    friend std::ostream& operator<<(std::ostream& os,const DynamicArray<U>& array);
private:
    T *a;
    int size;
    int capacity;
};
//类模板的函数在类外定义使，需要带上模板变量定义
//类名单独出现不算完整类型
template<typename T>
DynamicArray<T>::DynamicArray():size(0),capacity(15)
{
    a = new T[capacity];
}

template<typename T>
DynamicArray<T>::~DynamicArray()
{
    delete []a;
}

template <typename T>
void DynamicArray<T>::InsertTail(const T &element)
{
    if(size == capacity)
    {
        capacity *= 2;
        T *newP = new T[capacity];
        for(int i = 0;i < size;i++)
            newP[i] = a[i];
        delete []a;
        a = newP;
    }
    a[size++] = element;
}
template <typename T>
DynamicArray<T> &DynamicArray<T>::operator=(const DynamicArray& a)
{
    delete []this->a;
    this->size = a.size;
    this->capacity = a.capacity;
    this->a = new T[this->capacity];
    for(int i = 0;i < this->size;i++)
        this->a[i] = a.a[i];
    return *this;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const DynamicArray<T> &array)
{
    for(int i = 0;i< array.size;i++)
    {
        os<<array.a[i]<<" ";
    }
    os<<"\n";
    return os;
}
